// pages/banner_shop/banner_shop.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    low_img: "/statics/img/must_low_price.png",
    bow_price: "/statics/img/bow_price.png",
    spe_show: false,
    activeIndex: 0,
    cart_num: app.globalData.cart_num,
    allprice: 0,
    banner_shop:{},
    shop_list:[],
    istoday:false,
    isload:false,
    balls: [
      { isShow: false, animationData: null },
      { isShow: false, animationData: null }
    ]
  },
  choose_G: function (e) {
    var index = e.currentTarget.dataset.index
    var shop_list = this.data.shop_list
    if (shop_list[index].show_spece) {
      shop_list[index].show_spece = false
      shop_list[index].choose_text = "收起"
    } else {
      shop_list[index].show_spece = true
      shop_list[index].choose_text = "选规格"
    }
    this.setData({
      shop_list: shop_list
    })
  },
  add_cart: function (e) {
    app.ball_animate(e, this)
    var that = this
    var shop_list = this.data.shop_list
    var specval_id = e.currentTarget.dataset.specval_id
    for (let i in shop_list) {
      if (shop_list[i].specval_id == specval_id){
        shop_list[i].loading = true
      }
      for (let j in shop_list[i].list) {
        if (shop_list[i].list[j].specval_id == specval_id) {
          shop_list[i].list[j].loading = true
          break
        }
      }
      
    }
    this.setData({
      shop_list: shop_list
    })
    app.add_cart(e, this, function (id) {
      app.all_price(that)
      var shop_list = that.data.shop_list
      for (let i in shop_list) {
        if (shop_list[i].specval_id == specval_id) {
          shop_list[i].loading = false
        }
        for (let j in shop_list[i].list) {
          if (shop_list[i].list[j].specval_id == id) {
            shop_list[i].list[j].loading = false
            break
          }
        }
      }
      that.setData({
        shop_list: shop_list
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    if (options.adv_id){
      var adv_id = options.adv_id
      app.ajax({
        url: app.host + "mall/adv/advclick?adv_id=" + adv_id,
        success: function (res) {
          wx.hideLoading()
          wx.stopPullDownRefresh()
          var shop_list = res.data.data.list
          var banner_shop = res.data.data.adv
          if (shop_list.length == 1){
            that.setData({
              isload:true
            })
            wx.redirectTo({
              url: '/pages/shop_details/shop_details?goods_id=' + shop_list[0].goods_id,
            })
            return false;
          }
          for (let i in shop_list) {
            shop_list[i].index = i
            shop_list[i].show_spece = true
            shop_list[i].choose_text = "选规格"
          }
          console.log(shop_list)
          that.setData({
            banner_shop: banner_shop,
            shop_list: shop_list,
            istoday:false,
          })
        }
      })
    } else if (options.brand_id){
      app.ajax({
        url: app.host + 'mall/brand/brandClick?brand_id=' + options.brand_id,
        success:function(res){
          wx.hideLoading()
          wx.stopPullDownRefresh()
          var shop_list = res.data.data.list
          // var banner_shop = res.data.data.adv
          for (let i in shop_list) {
            shop_list[i].index = i
            shop_list[i].show_spece = true
            shop_list[i].choose_text = "选规格"
          }
          that.setData({
            // banner_shop: banner_shop,
            shop_list: shop_list,
            istoday: false
          })
        }
      })
    }else{
      app.ajax({
        url: app.host + 'mall/goods/todaycutprice',
        success:function(res){
          wx.hideLoading()
          wx.stopPullDownRefresh()
          var shop_list = res.data.data.list
          // var banner_shop = res.data.data.adv
          for (let i in shop_list) {
            shop_list[i].index = i
            shop_list[i].show_spece = true
            shop_list[i].choose_text = "选规格"
          }
          that.setData({
            // banner_shop: banner_shop,
            shop_list: shop_list,
            istoday:true
          })
        }
      })
    }
  },
  add_freq: function (e) {
    app.ajax({
      url: app.host + 'mall/Freqlist/add_freq',
      data: {
        goods_id: e.currentTarget.dataset.goods_id
      },
      success: function (res) {
        console.log(res);
          wx.showToast({
            title: res.data.msg,
          })
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.cart_num(this)
    app.all_price(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})